package ua.kharkov.knure.pereverziev.diploma.db.DAOServices;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ua.kharkov.knure.pereverziev.diploma.db.entity.Airport;
import ua.kharkov.knure.pereverziev.diploma.db.entity.Flight;
import ua.kharkov.knure.pereverziev.diploma.db.entity.Route;
import ua.kharkov.knure.pereverziev.diploma.web.util.DateTimeUtil;

public class FlightsService extends CommonService<Flight> {

	private final Logger log = LoggerFactory.getLogger(FlightsService.class);
	private static SimpleDateFormat formatter = new SimpleDateFormat(
			"yyyy-MM-dd");

	public FlightsService() {
		super(Flight.class);
	}

	@SuppressWarnings("unchecked")
	public List<Route> getFlights(final Date date, final Airport departure,
			final Airport destination) {
		final List<Route> result = new ArrayList<Route>();

		final String subQuerry = "select shed.id from ua.kharkov.knure.pereverziev.diploma.db.entity.Schedule as shed where shed.departure.id="
				+ departure.getId()
				+ " and shed.destination.id="
				+ destination.getId();

		final String querry = "select flight.id from ua.kharkov.knure.pereverziev.diploma.db.entity.Flight flight where flight.schedule.id in ("
				+ subQuerry
				+ ") and flight.flightDate='"
				+ DateTimeUtil.getDateString(date).replace(" ", "") + "'";

		final List selectList = HibernateUtils.getSession().createQuery(querry).list();

		String ids = "";

		for (final Object id : selectList) {
			ids += "," + id;
		}
		List<Flight> flights = null;
		if (!ids.isEmpty()) {

			ids = ids.substring(1);
			log.debug("HSQL " + selectList.size() + " Data: " + selectList
					+ " ids=" + ids);

			flights = HibernateUtils
					.getSession()
					.createQuery(
							"from ua.kharkov.knure.pereverziev.diploma.db.entity.Flight as flight "
									+ "where flight.id in (" + ids + ")")
					.list();

			log.debug("Found " + result.size()
					+ " flights. Conditions: \ndate: " + date
					+ "\n Departure airport:" + departure
					+ "\nDestination airport:" + destination);
		}

		if (flights == null)
			flights = Collections.emptyList();
		for (final Flight flight : flights) {
			result.add(new Route(flight));
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<Flight> getDeparturesFlights(final Airport airport) {

		final Calendar endDate = Calendar.getInstance();
		endDate.add(Calendar.DAY_OF_YEAR, 7);

		final String querry = "FROM "
				+ "ua.kharkov.knure.pereverziev.diploma.db.entity.Flight flight "
				+ "WHERE flight.schedule.departure="
				+ airport.getId()
				+ " AND flight.flightDate BETWEEN '"
				+ formatter.format(new java.util.Date())
				+ "' AND '"
				+ formatter.format(endDate.getTime())
				+ "' order by flight.flightDate asc, flight.schedule.departureTime asc";

		return HibernateUtils.getSession().createQuery(querry).list();
	}

	@SuppressWarnings("unchecked")
	public List<Flight> getArrivalsFlights(final Airport airport) {

		final Calendar endDate = Calendar.getInstance();
		endDate.add(Calendar.DAY_OF_YEAR, 7);
		final String querry = "FROM "
				+ "ua.kharkov.knure.pereverziev.diploma.db.entity.Flight flight "
				+ "WHERE flight.schedule.destination="
				+ airport.getId()
				+ " AND flight.flightDate BETWEEN '"
				+ formatter.format(new java.util.Date())
				+ "' AND '"
				+ formatter.format(endDate.getTime())
				+ "' order by flight.flightDate asc, flight.schedule.departureTime asc";

		return HibernateUtils.getSession().createQuery(querry).list();
	}

	@SuppressWarnings("unchecked")
	public List<Flight> getDeparturesFlights(final Airport airport,
			final Date date) {
		final Calendar endDate = Calendar.getInstance();
		endDate.setTime(date);
		endDate.add(Calendar.DAY_OF_YEAR, 1);
		final String querry = "FROM "
				+ "ua.kharkov.knure.pereverziev.diploma.db.entity.Flight flight "
				+ "WHERE flight.schedule.departure="
				+ airport.getId()
				+ " AND flight.flightDate BETWEEN '"
				+ formatter.format(date)
				+ "' AND '"
				+ formatter.format(endDate.getTime())
				+ "' order by flight.flightDate asc, flight.schedule.departureTime asc";

		return HibernateUtils.getSession().createQuery(querry).list();
	}

	@SuppressWarnings("unchecked")
	public List<Flight> getDestinationFlights(final Airport airport,
			final Date date) {
		final Calendar endDate = Calendar.getInstance();
		endDate.setTime(date);
		endDate.add(Calendar.DAY_OF_YEAR, 2);
		final String querry = "FROM "
				+ "ua.kharkov.knure.pereverziev.diploma.db.entity.Flight flight "
				+ "WHERE flight.schedule.destination="
				+ airport.getId()
				+ " AND flight.flightDate BETWEEN '"
				+ formatter.format(date)
				+ "' AND '"
				+ formatter.format(endDate.getTime())
				+ "' order by flight.flightDate asc, flight.schedule.departureTime asc";

		return HibernateUtils.getSession().createQuery(querry).list();
	}
}
